﻿Imports System
Imports System.Windows
Imports System.Windows.Forms
Imports System.Drawing



Public Class MenuButtonEmulsifier

	Public Event EmulsionClick(sender As Object, e As EventArgs)

	Public Delegate Sub EmulsionClickDelegate(sender As Object, e As EventArgs)



	Protected oMenuItem As ToolStripMenuItem = Nothing

	Protected oButton As ToolStripButton = Nothing

	Protected oIconImage As Image = Nothing

	Protected bShowMenuIcon As Boolean = False

	Protected bShowButtonIcon As Boolean = False

	Protected oShortcutKeys As List(Of Keys) = New List(Of Keys)



	Public Sub New()

	End Sub

	Public Sub New(menuItem As ToolStripMenuItem, button As ToolStripButton)

		Me.New()

		RemoveMenuShortcutKey()
		ClearHandlers()

		oMenuItem = menuItem
		oButton = button

		AddMenuShortcutKey()
		RewireHandlers()

	End Sub

	Public Sub New(menuItem As ToolStripMenuItem, button As ToolStripButton, clickHandler As EmulsionClickDelegate)

		Me.New(menuItem, button)

		Me.EmulsionClickHandler = clickHandler

	End Sub



	Public Sub AddShortcutKey(ByVal tKey As Keys)

		If Not oShortcutKeys.Contains(tKey) Then
			oShortcutKeys.Add(tKey)
		End If

	End Sub



	Private Sub AddMenuShortcutKey()

		If Not Nothing Is oMenuItem Then
			If Keys.None <> oMenuItem.ShortcutKeys Then
				AddShortcutKey(oMenuItem.ShortcutKeys)
			End If
		End If

	End Sub



	Public Sub RemoveShortcutKey(ByVal tKey As Keys)

		oShortcutKeys.Remove(tKey)

	End Sub



	Private Sub RemoveMenuShortcutKey()

		If Not Nothing Is oMenuItem Then
			RemoveShortcutKey(oMenuItem.ShortcutKeys)
		End If

	End Sub



	Public Function CanHandleShortcutKey(ByVal tKey As Keys) As Boolean

		Return oShortcutKeys.Contains(tKey)

	End Function



	Public Function PleaseHandleShortcutKey(ByVal tKey As Keys) As Boolean

		Dim tResult As Boolean = CanHandleShortcutKey(tKey)

		If tResult Then
			InvokeClick()
		End If

		Return tResult

	End Function



	Protected Sub SetupEmulsion()

		If (Not Nothing Is oMenuItem) Then
			If (ShowMenuIcon) Then
				oMenuItem.Image = Me.IconImage
			Else
				oMenuItem.Image = Nothing
			End If
		End If

		If (Not Nothing Is oButton) Then
			If (ShowButtonIcon) Then
				oButton.Image = Me.IconImage
			Else
				oButton.Image = Nothing
			End If
		End If

	End Sub



	Protected Sub ClearHandlers()

		If Not Nothing Is oMenuItem Then
			RemoveHandler oMenuItem.Click, AddressOf MenuClickHandler
		End If

		If Not Nothing Is oButton Then
			RemoveHandler oButton.Click, AddressOf ButtonClickHandler
		End If

	End Sub



	Protected Sub RewireHandlers()

		If Not Nothing Is oMenuItem Then
			AddHandler oMenuItem.Click, AddressOf MenuClickHandler
		End If

		If Not Nothing Is oButton Then
			AddHandler oButton.Click, AddressOf ButtonClickHandler
		End If

	End Sub



	Private Sub MenuClickHandler(sender As Object, e As EventArgs)

		OnEmulsionClick()

	End Sub



	Private Sub ButtonClickHandler(sender As Object, e As EventArgs)

		OnEmulsionClick()

	End Sub



	Protected Overridable Sub OnEmulsionClick()

		If Not Nothing Is Me.EmulsionClickHandler Then
			Me.EmulsionClickHandler.Invoke(Me, New EventArgs)
		Else
			RaiseEvent EmulsionClick(Me, New EventArgs)
		End If

	End Sub



	Public Sub InvokeClick()

		OnEmulsionClick()

	End Sub



	Public Property MenuItem As ToolStripMenuItem
		Get
			Return oMenuItem
		End Get
		Set(value As ToolStripMenuItem)
			RemoveMenuShortcutKey()
			SetupEmulsion()
			oMenuItem = value
			AddMenuShortcutKey()
			RewireHandlers()
		End Set
	End Property



	Public Property Button As ToolStripButton
		Get
			Return oButton
		End Get
		Set(value As ToolStripButton)
			SetupEmulsion()
			oButton = value
			RewireHandlers()
		End Set
	End Property



	Public Property EmulsionClickHandler As EmulsionClickDelegate



	Public Property ShowMenuIcon As Boolean
		Get
			Return bShowMenuIcon
		End Get
		Set(value As Boolean)
			bShowMenuIcon = value
			SetupEmulsion()
		End Set
	End Property



	Public Property ShowButtonIcon As Boolean
		Get
			Return bShowButtonIcon
		End Get
		Set(value As Boolean)
			bShowButtonIcon = value
			SetupEmulsion()
		End Set
	End Property



	Public Property ShowIcons As Boolean
		Get
			Return (ShowMenuIcon) And (ShowButtonIcon)
		End Get
		Set(value As Boolean)
			bShowMenuIcon = value
			bShowButtonIcon = value
			SetupEmulsion()
		End Set
	End Property



	Public Property IconImage As Image
		Get
			Return oIconImage
		End Get
		Set(value As Image)
			oIconImage = value
			SetupEmulsion()
		End Set
	End Property


	Public ReadOnly Property ShortcutKeys As List(Of Keys)
		Get
			Return oShortcutKeys
		End Get
	End Property

End Class
